package com.clever.springssm.controller;

import com.clever.springssm.entity.Carts;
import com.clever.springssm.entity.Product;
import com.clever.springssm.request.CartsRequestParams;
import com.clever.springssm.service.CartsService;
import com.clever.springssm.utils.ResponseEntity;
import com.clever.springssm.vo.CartsVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ChenWang
 * @className CartsController
 * @date 2020/10/30 14:27
 * @since JDK 1.8
 */
/**
 * 1.添加购物车
 * 1.1如果购物车商品已经存在，直接数量相加
 * 1.2如果购物车商品不存在，直接添加
 * 2.查看购物车
 * 3.删除购物车
 * 4修改商品的数量
 * 5.加入收藏夹
 */
@RestController
@RequestMapping("/cart")
public class CartsController {
    @Resource
    private CartsService cartsService;
    /**
     * 通过用户ID查询所有的购物车信息
     * @param userId  指定的用户ID
     * @return  List<CartsVo>  用于展示的购物车信息的集合
     * @author ChenWang
     * @date 2020/10/30 14:30
     */
    @GetMapping("/list")
    ResponseEntity<List<CartsVo>> seletAll(@RequestParam("userId") int userId){
        try{
            List<CartsVo> cartsVoList = cartsService.seletAllByUserId(userId);
            return ResponseEntity.success(cartsVoList);
        }catch(Exception e){
            return ResponseEntity.error();
        }
    }

    @PostMapping("/add")
    ResponseEntity<Boolean> add(@RequestParam("carts") CartsRequestParams cartsRequestParams){
        try{
            int i = cartsService.saveCarts(cartsRequestParams);
            if(i>0){
                return ResponseEntity.success(true);
            }else{
                return ResponseEntity.error();
            }
        }catch (Exception e){
            return ResponseEntity.error();
        }
    }

    @PutMapping("/del")
    ResponseEntity<Boolean> del(@RequestParam List<Integer> cartIds ){
        try{
            int i = cartsService.delCarsById(cartIds);
            if(i>0){
                return ResponseEntity.success(true);
            }else{
                return ResponseEntity.error();
            }
        }catch (Exception e){
            return ResponseEntity.error();
        }
    }

    @PutMapping("/update")
    ResponseEntity<Boolean> update(@RequestParam List<CartsRequestParams> cartsRequestParams){
        try{
            int i = cartsService.update(cartsRequestParams);
            if(i>0){
                return ResponseEntity.success(true);
            }else{
                return ResponseEntity.error();
            }
        }catch (Exception e){
            return ResponseEntity.error();
        }
    }
}
